package cn.tedu.ivos.geofence.service.serviceImpl;

import cn.tedu.ivos.geofence.mapper.GeofenceMapper;
import cn.tedu.ivos.geofence.pojo.dto.GeofenceParam;
import cn.tedu.ivos.geofence.pojo.dto.GeofenceQuery;
import cn.tedu.ivos.geofence.pojo.entity.Geofence;
import cn.tedu.ivos.geofence.pojo.vo.GeofenceVO;
import cn.tedu.ivos.geofence.service.GeofenceService;
import cn.tedu.ivos.vehicle.mapper.VehicleMapper;
import cn.tedu.ivos.vehicle.pojo.dto.VehicleQuery;
import cn.tedu.ivos.vehicle.pojo.vo.VehicleVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class GeofenceServiceImpl implements GeofenceService {
    @Autowired
    private GeofenceMapper geofenceMapper;
    @Autowired
    private VehicleMapper vehicleMapper;
    @Override
    public List<GeofenceVO> selectGeofenceTab(GeofenceQuery geofenceQuery) {
        log.debug("开始查询围栏信息"+geofenceQuery);
        List<GeofenceVO> geofenceVOSList = geofenceMapper.selectGeofenceTab(geofenceQuery);
        for(GeofenceVO geofenceVO:geofenceVOSList){
            VehicleQuery vehicleQuery = new VehicleQuery();
            vehicleQuery.setGeofenceId(geofenceVO.getId());
            List<VehicleVo> vehicleVoList = vehicleMapper.selectVehicleTableMap(vehicleQuery);
            geofenceVO.setTotalNum(vehicleVoList.size());
            geofenceVO.setVehicleVoList(vehicleVoList);
            int availableNum = 0;
            for(VehicleVo vehicleVo:vehicleVoList){
                if(vehicleVo.getStatus().equals("1")){
                    availableNum++;
                }
            }
//            vehicleQuery.setStatus("1");
//            List<VehicleVo> vehicleVoList1 = vehicleMapper.selectVehicleTableMap(vehicleQuery);
            geofenceVO.setAvailableNum(availableNum);
        }
        return geofenceVOSList;
    }

    @Override
    public int addGeofenceTab(GeofenceParam geofenceParam) {
        log.debug("开始添加围栏信息"+geofenceParam);
        int rows=0;
        Geofence geofence = new Geofence();
        BeanUtils.copyProperties(geofenceParam,geofence);
        if(geofence.getId()==null){
            geofence.setCreateTime(new Date());
            geofence.setStatus("1");
            rows = geofenceMapper.insertGeofenceTab(geofence);

        }else {
            geofence.setUpdateTime(new Date());
            rows = geofenceMapper.updateGeofenceTab(geofence);
        }

        return rows;

    }

    @Override
    public int deleteGeofenceTab(Long id) {
        log.debug("开始删除围栏信息"+id);
        VehicleQuery vehicleQuery = new VehicleQuery();
        vehicleQuery.setGeofenceId(id);
        List<VehicleVo> vehicleVoList= vehicleMapper.selectVehicleTableMap(vehicleQuery);
        if(vehicleVoList.size()==0){
            int rows = geofenceMapper.deleteGeofenceTab(id);
            return rows;
        }
        return -1;
    }
}
